package com.bbseguros.pesquisador.action;


import java.util.List;

import org.apache.log4j.Logger;

import com.bbseguros.pesquisador.bean.DataBean;
import com.bbseguros.pesquisador.bean.UserBean;
import com.bbseguros.pesquisador.middle.UserMiddle;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;



/**
 * @author hmsilva
 *
 */
public class UserAction extends ActionSupport implements Preparable{

	private static final long serialVersionUID = 1L;	
	private static Logger logger = Logger.getLogger(UserAction.class);
	
	private UserBean user;
	private String message;
	private List <UserBean> listUsers;
	private List <DataBean> listPerfil;
	private List <DataBean> listGrupo;
	private String functionType;
	private int groupId;
	
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	public void prepare() throws Exception {
		listGrupo = UserMiddle.getListGrupo();
		listPerfil = UserMiddle.getListPerfil();		
	}
	
	
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#validate()
	 */
	public void validate() {
		if(getUser()!=null) {
			if(getUser().getUsuario()!=null) {
				if (getUser().getUsuario().length() == 0) {
					addFieldError("user.nome", getText("nome.required"));
				}
			}
			if(getUser().getEmail()!=null) {
				if (getUser().getEmail().length() == 0) {
					addFieldError("user.email", getText("email.required"));
				}
			}
			if(getUser().getSenha()!=null) {
				if (getUser().getSenha().length() == 0) {
					addFieldError("user.senha", getText("senha.required"));
				}
			}
			if(getUser().getConfirmarSenha()!=null) {
				if (getUser().getConfirmarSenha().length() == 0) {
					addFieldError("user.confirmarsenha", getText("confirmarsenha.required"));
				}else if(!getUser().getConfirmarSenha().equals(getUser().getSenha())){
					addFieldError("user.confirmarsenha", getText("user.passwordError"));				}
			}
			if(getUser().getPerfil()!=null) {
				if (getUser().getPerfil().length() == 0) {
					addFieldError("user.perfil", getText("perfil.required"));
				}
			}			
		}
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute(){
		
		listUsers = UserMiddle.getListUsers();		
		return "viewAll";
	}
	
	
	/**
	 * @return
	 */
	public String list(){
		
		listUsers = UserMiddle.getListUsers();
		return "viewAll";
	}
	
	/**
	 * @return
	 */
	public String listByGroup(){
		logger.debug("GroupId: "+groupId);
		listUsers = UserMiddle.getListUsers(groupId);
		return "viewAll";
	}	
	
	/**
	 * @return
	 */
	public String status(){
		
		String status = user.getStatus();
		logger.debug("status received: "+status);
		if(status.equals("0")){
			status = "1";
		}else{
			status = "0";
		}
		logger.debug("status: "+status+" id: "+user.getId());
		
		if(UserMiddle.changeStatus(String.valueOf(user.getId()), status)){
			listUsers = UserMiddle.getListUsers();
			return "viewAll";	
			
		}else{
			return ERROR;
		}
		
	}
	
	/**
	 * @return
	 */
	public String create(){		
		setFunctionType("cadastro");
		
		listGrupo = UserMiddle.getListGrupo();
		listPerfil = UserMiddle.getListPerfil();
				
		return "enterData";
	}

	/**
	 * @return
	 */
	public String insert() {	
		if(user.getIdPerfil() == -1){
			addActionError(getText("perfil.required"));
			return ERROR;
			
		}else if(user.getIdGrupo() == -1){
			addActionError(getText("grupo.required"));
			return ERROR;
			
		}else if(UserMiddle.verifyUser(user.getUsuario())){
			addActionError(getText("user.thereIs"));
			return ERROR;
			
		}else if(UserMiddle.verifyCodUser(user.getCodigo_usuario())){
			addActionError(getText("codUser.thereIs"));
			return ERROR;
			
		}else{
			user.setPerfil(UserMiddle.getPerfilByID((String.valueOf(user.getIdPerfil()))).getValue());
			user.setGrupo(UserMiddle.getGrupoByID((String.valueOf(user.getIdGrupo()))).getValue());
			
			return "confirmData";	
		}		
	}
	
	/**
	 * @return
	 */
	public String insertConfirm() {	
		logger.debug("USER: "+user.toString());
		
		if(UserMiddle.insertUser(user)){
			return SUCCESS;	
		}else{
			addActionError(getText("user.insertError"));
			return ERROR;
		}		
	}
	
	/**
	 * @return
	 */
	public String edit() {
		setFunctionType("update");
		
		int id = getUser().getId();
		user = UserMiddle.selectUserByID(String.valueOf(id));
		
		return "enterData";		
	}	
	
	/**
	 * @return
	 */
	public String update() {
		setFunctionType("update");	
		
		logger.debug("USER: "+user.toString());
		
		user.setPerfil(UserMiddle.getPerfilByID((String.valueOf(user.getIdPerfil()))).getValue());
		user.setGrupo(UserMiddle.getGrupoByID((String.valueOf(user.getIdGrupo()))).getValue());
				
		return "confirmData";
	}	

	/**
	 * @return
	 */
	public String updateConfirm() {
		logger.debug("USER: "+user.toString());
		
		if(UserMiddle.updateUser(user)){
			return SUCCESS;	
		}else{
			addActionError(getText("user.deleteUserError"));
			return ERROR;
		}		
	}	
	
	
	/**
	 * @return
	 */
	public String delete(){
		int id = getUser().getId();		
		this.user = UserMiddle.selectUserByID(String.valueOf(id));
						
		user.setPerfil(UserMiddle.getPerfilByID((String.valueOf(user.getIdPerfil()))).getValue());
		user.setGrupo(UserMiddle.getGrupoByID((String.valueOf(user.getIdGrupo()))).getValue());
		
		setFunctionType("delete");
		return "confirmData";		
	}
	
	/**
	 * @return
	 */
	public String deleteConfirm() {
		logger.debug("USER: "+user.toString());
		
		if(UserMiddle.deleteUser(user.getId())){
			return SUCCESS;	
		}else{
			addActionError(getText("user.deleteUserError"));
			return ERROR;
		}		
	}	

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List <UserBean> getListUsers() {
		return listUsers;
	}
	
	public String getFunctionType() {
		return functionType;
	}

	public void setFunctionType(String functionType) {
		this.functionType = functionType;
	}

	public List<DataBean> getListPerfil() {
		return listPerfil;
	}

	public void setListPerfil(List<DataBean> listPerfil) {
		this.listPerfil = listPerfil;
	}

	public List<DataBean> getListGrupo() {
		return listGrupo;
	}

	public void setListGrupo(List<DataBean> listGrupo) {
		this.listGrupo = listGrupo;
	}


	public int getGroupId() {
		return groupId;
	}


	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}
}
